package com.hspedu.newData.dmsxl.字符串;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-09 
 * @Version: 1.0
 */

public class LC6Z字形变换 {

    // todo
    // 利用标记位，自动变换累加方向
    public String convert(String s, int numRows) {

        if (numRows < 2) return s;

        List<StringBuilder> row = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            row.add(new StringBuilder());
        }

        char[] charArray = s.toCharArray();

        int i = 0;
        int flag = -1;
        for (char c : charArray) {
            if (i == 0 || i == numRows - 1) flag = -flag;
            row.get(i).append(c);
            i += flag;
        }

        StringBuilder res = new StringBuilder();
        for (int j = 0; j < numRows; j++) {
            res.append(row.get(j));
        }

        return res.toString();
    }


    @Test
    public void test() {
        // 测试代码

        convert("PAYPALISHIRING",3);

    }


}
